#include "filenameformat.h"

#include <QtGui>
#include "track.h"
#include "ui_filenameformat.h"

FilenameFormat::FilenameFormat(QWidget* parent)
: QDialog(parent), ui(new Ui::FilenameFormat())
{
    this->track_[DataObjectType::ARTIST] = "Pink Floyd";
    this->track_[DataObjectType::ALBUM] = "The Wall";
    this->track_[DataObjectType::TITLE] = "Is There Anybody Out There?";
    this->track_[DataObjectType::GENRE] = "Progressive rock";
    this->track_[DataObjectType::LOCATION] = "xxx.mp3";
    this->track_.year = 1979;
    this->track_.disk_number = 3;
    this->track_.total_disks = 4;
    this->track_.track_number = 2;
    this->track_.total_tracks = 6;

    ui->setupUi(this);

    QSettings settings;

    this->set_output_directory(settings.value(
        "FilenameFormat/output_directory",
        QDir::homePath()).toString());
    this->set_format(settings.value(
        "FilenameFormat/format",
        "<artist>/<album> (<year>)/<track_number,2>. <title>").toString());
}

FilenameFormat::~FilenameFormat()
{
    delete ui;
}

QString const & FilenameFormat::format() const
{
    return this->format_;
}

QString const & FilenameFormat::output_directory() const
{
    return this->output_directory_;
}

void FilenameFormat::choose_directory()
{
    QString const directory = QFileDialog::getExistingDirectory(
        this, tr("Select destination directory"), this->output_directory_);
    if(directory != "")
    {
        this->set_output_directory(directory);
    }
}

void FilenameFormat::set_output_directory(QString const & output_directory)
{
    this->output_directory_ = output_directory;
    QSettings settings;
    settings.setValue("FilenameFormat/output_directory", output_directory);
    this->ui->output_directory->setText(output_directory);
}

void FilenameFormat::set_format(QString const & format)
{
    this->format_ = format;
    int const cursorPosition = this->ui->format->cursorPosition();
    this->ui->format->setText(format);
    this->ui->format->setCursorPosition(cursorPosition);
    QSettings settings;
    settings.setValue("FilenameFormat/format", format);

    bool format_correct = true;
    QString filename;
    QString error_message;
    try
    {
        filename = this->track_.filename(this->format_);
    }
    catch(std::exception const & e)
    {
        format_correct = false;
        error_message = e.what();
    }

    if(format_correct)
    {
        this->ui->sample->setText(filename);
        this->ui->format->setStyleSheet("QLineEdit {}");
    }
    else
    {
        this->ui->sample->setText("(invalid format : "+error_message+")");
        this->ui->format->setStyleSheet("QLineEdit { background-color: yellow }");
    }
}

void FilenameFormat::add_album()
{
    QString format = this->format();
    format.insert(this->ui->format->cursorPosition(), "<album>");
    this->set_format(format);
}

void FilenameFormat::add_artist()
{
    QString format = this->format();
    format.insert(this->ui->format->cursorPosition(), "<artist>");
    this->set_format(format);
}

void FilenameFormat::add_genre()
{
    QString format = this->format();
    format.insert(this->ui->format->cursorPosition(), "<genre>");
    this->set_format(format);
}

void FilenameFormat::add_title()
{
    QString format = this->format();
    format.insert(this->ui->format->cursorPosition(), "<title>");
    this->set_format(format);
}

void FilenameFormat::add_track_number()
{
    QString format = this->format();
    format.insert(this->ui->format->cursorPosition(), "<track_number>");
    this->set_format(format);
}

void FilenameFormat::add_year()
{
    QString format = this->format();
    format.insert(this->ui->format->cursorPosition(), "<year>");
    this->set_format(format);
}
